//
//  CYHomeBannerCell.m
//  cyjf
//
//  Created by tauCross on 2017/5/4.
//  Copyright © 2017年 Auto Financing. All rights reserved.
//

#import "CYHomeBannerCell.h"

#import <SDCycleScrollView.h>
#import <UIImageView+WebCache.h>



@implementation CYHomeBannerCellObject

@end



@interface CYHomeBannerCell ()<SDCycleScrollViewDelegate>

@property(nonatomic, strong)SDCycleScrollView *cycelScrollView;
@property(nonatomic, strong)UIView *itemView;
@property(nonatomic, strong)UIView *itemLine;
@property(nonatomic, strong)UIImageView *noticeImageView;
@property(nonatomic, strong)UILabel *noticeLabel;
@property(nonatomic, strong)UIImageView *noticeArrow;
@property(nonatomic, strong)UIButton *noticeButton;
@property(nonatomic, strong)UIView *itemContentView;
@property(nonatomic, strong)NSMutableArray<RACDisposable *> *itemDisposables;

@property(nonatomic, strong)CYHomeBannerCellObject *object;

@end


@implementation CYHomeBannerCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self)
    {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = [UIColor clearColor];
        
        self.itemDisposables = [NSMutableArray array];
        
        self.cycelScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectZero delegate:self placeholderImage:nil];
        self.cycelScrollView.bannerImageViewContentMode = UIViewContentModeScaleAspectFill;
        self.cycelScrollView.pageControlBottomOffset = 15;
        [self.contentView addSubview:self.cycelScrollView];
        
        self.itemView = [[UIView alloc] init];
        self.itemView.backgroundColor = [UIColor whiteColor];
        self.itemView.layer.masksToBounds = YES;
        self.itemView.layer.cornerRadius = 5;
        [self.contentView addSubview:self.itemView];
        
        self.itemLine = [UIView cy_lineWithColor:HEXCOLOR(@"DDDDDD")];
        [self.contentView addSubview:self.itemLine];
        
        self.noticeImageView = [[UIImageView alloc] initWithImage:CYImage(@"ico_index_notice_horn")];
        [self.contentView addSubview:self.noticeImageView];
        
        self.noticeLabel = [[UILabel alloc] init];
        self.noticeLabel.textColor = HEXCOLOR(@"666666");
        self.noticeLabel.font = [UIFont systemFontOfSize:12];
        [self.contentView addSubview:self.noticeLabel];
        
        self.noticeArrow = [[UIImageView alloc] initWithImage:CYImage(@"public_cell_arrow")];
        [self.contentView addSubview:self.noticeArrow];
        
        self.noticeButton = [[UIButton alloc] init];
        [self.noticeButton addTarget:self action:@selector(onNoticeButtonTouchUpInside) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:self.noticeButton];
        
        self.itemContentView = [[UIView alloc] init];
        [self.itemView addSubview:self.itemContentView];
    }
    return self;
}

+ (CGFloat)heightForObject:(CYHomeBannerCellObject *)object atIndexPath:(NSIndexPath *)indexPath tableView:(UITableView *)tableView
{
    return tableView.width * 220 / 375 + (object.noticeModel ? 120 : 70);
}

- (BOOL)shouldUpdateCellWithObject:(CYHomeBannerCellObject *)object
{
    self.object = object;
    
    NSMutableArray *bannerUrls = [NSMutableArray array];
    for(CYDataAPIBannerModel *model in object.bannerModels)
    {
        [bannerUrls addObject:model.imageUrl];
    }
    self.cycelScrollView.imageURLStringsGroup = bannerUrls;
    
    self.noticeButton.hidden =
    self.noticeArrow.hidden =
    self.noticeLabel.hidden =
    self.noticeImageView.hidden =
    self.itemLine.hidden =
    object.noticeModel == nil;
    
    if(self.object.noticeModel)
    {
        self.noticeLabel.text = object.noticeModel.title;
    }

    [self reloadItemContentView];

    self.cycelScrollView.autoScrollTimeInterval = 3;
    self.cycelScrollView.autoScroll = YES;
    
    [self setNeedsLayout];
    return YES;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    self.cycelScrollView.width = self.contentView.width;
    self.cycelScrollView.height = self.contentView.width * 220 / 375;
    self.cycelScrollView.left = 0;
    self.cycelScrollView.top = 0;
    
    self.itemView.width = self.contentView.width - 20;
    self.itemView.height = self.object.noticeModel ? 140 : 90;
    self.itemView.bottom = self.contentView.height;
    self.itemView.centerX = self.contentView.width / 2;
    
    self.itemLine.width = self.itemView.width - 40;
    self.itemLine.height = 1 / SCREEN_SCALE;
    self.itemLine.centerX = self.itemView.centerX;
    self.itemLine.centerY = self.itemView.top + 90;
    
    self.noticeImageView.centerY = self.contentView.height - 25;
    self.noticeImageView.left = 30;
    
    self.noticeArrow.centerY = self.noticeImageView.centerY;
    self.noticeArrow.right = self.contentView.width - 22;
    
    [self.noticeLabel sizeToFit];
    self.noticeLabel.width = self.noticeArrow.left - 6 - self.noticeImageView.right - 6;
    self.noticeLabel.centerY = self.noticeImageView.centerY;
    self.noticeLabel.left = self.noticeImageView.right + 6;
    
    self.noticeButton.width = self.itemView.width;
    self.noticeButton.height = 50;
    self.noticeButton.centerX = self.itemView.centerX;
    self.noticeButton.centerY = self.contentView.height - 25;
    
    if(self.itemContentView.width != self.itemView.width)
    {
        self.itemContentView.width = self.itemView.width;
        self.itemContentView.height = 90;
        self.itemContentView.left = 0;
        self.itemContentView.top = 0;
    }
}

#pragma mark - Control Events
- (void)onNoticeButtonTouchUpInside
{
    if(self.object.onClickNoticeBlock)
    {
        self.object.onClickNoticeBlock(self.object.noticeModel);
    }
}

- (void)onItemButtonTouchUpInside:(UIButton *)button
{
    if(self.object.itemModels.count >= button.tag)
    {
        if(self.object.onClickBannerBlock)
        {
            [MobClick event:UM_EVENT_0012 label:SAFE_NUMBER(self.object.itemModels[button.tag].id).stringValue];
            self.object.onClickBannerBlock(self.object.itemModels[button.tag].action);
        }
    }
}

#pragma mark - SDCycleScrollViewDelegate
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    if(index < self.object.bannerModels.count)
    {
        if(self.object.onClickBannerBlock)
        {
            [MobClick event:UM_EVENT_0011 label:SAFE_NUMBER([self.object.bannerModels[index] id]).stringValue];
            self.object.onClickBannerBlock(self.object.bannerModels[index].action);
        }
    }
}

#pragma mark - Private Method
- (void)reloadItemContentView
{
    [self.itemContentView removeAllSubView];
    for(RACDisposable *disposable in self.itemDisposables)
    {
        [disposable dispose];
    }
    [self.itemDisposables removeAllObjects];
    for(CYDataAPIAdvertisementModel *model in self.object.itemModels)
    {
        @weakify(self);
        @weakify(model);
        UIButton *item = [[UIButton alloc] init];
        item.tag = [self.object.itemModels indexOfObject:model];
        [item addTarget:self action:@selector(onItemButtonTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
        [self.itemContentView addSubview:item];
        @weakify(item);
        [self.itemDisposables addObject:[TCRACObserve(self.itemContentView, frame) subscribeNext:^(id x) {
            @strongify(self);
            @strongify(item);
            @strongify(model);
            
            item.width = self.itemContentView.width / self.object.itemModels.count;
            item.height = self.itemContentView.height;
            item.left = item.width * [self.object.itemModels indexOfObject:model];
            item.top = 0;
        }]];
        
        UIImageView *itemImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
        [itemImageView sd_setImageWithURL:model.image.tc_url];
        [item addSubview:itemImageView];
        
        UILabel *itemLabel = [[UILabel alloc] init];
        itemLabel.text = model.title;
        itemLabel.font = [UIFont systemFontOfSize:12];
        itemLabel.textColor = HEXCOLOR(@"666666");
        [itemLabel sizeToFit];
        [item addSubview:itemLabel];
        
        @weakify(itemLabel);
        @weakify(itemImageView);
        [self.itemDisposables addObject:[TCRACObserve(item, frame) subscribeNext:^(id x) {
            @strongify(item);
            @strongify(itemLabel);
            @strongify(itemImageView);

            itemImageView.centerX = item.width / 2;
            itemImageView.top = 15;
            
            itemLabel.centerX = item.width / 2;
            itemLabel.centerY = item.height - 19;
        }]];

    }
}

@end
